//------------------------------------------------------------------------------
//  <copyright file="LineSegment2DTests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Test.Robotics.Geometry
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Geometry;
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Tests for Plane2DClass
    /// </summary>
    [TestClass]
    public class LineSegment2DTests
    {
        /// <summary>
        /// Test the constructor for Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void LineSegment2DConstrutorTest()
        {
            Point2D pointA = new Point2D(3, 5);
            Point2D pointB = new Point2D(-3, 7);

            LineSegment2D lineSegment = new LineSegment2D(pointA, pointB);

            Assert.AreEqual(pointA, lineSegment.EndpointA);
            Assert.AreEqual(pointB, lineSegment.EndpointB);
        }

        /// <summary>
        /// Test intersection of line segment and line
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void LineSegment2DLineIntersectionTest()
        {
            // Two segements on the same line, intersection will be within one and not the other
            LineSegment2D segment1 = new LineSegment2D(new Point2D(0, 3), new Point2D(3, 0));
            LineSegment2D segment2 = new LineSegment2D(new Point2D(0, 3), new Point2D(-1, 4));

            Line2D line = new Line2D(new Point2D(-2, -3), new Point2D(1, 2));

            List<Point2D> segment1Intersection = segment1.GetIntersectionPoints(line);
            List<Point2D> segment2Intersection = segment2.GetIntersectionPoints(line);

            Assert.AreEqual(new Point2D(1, 2), segment1Intersection[0]);
            Assert.AreEqual(0, segment2Intersection.Count);
        }

        /// <summary>
        /// Test intersection of line segment and circle at two points
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void LineSegment2DCircleAtTwoPointsIntersectionTest()
        {
            Point2D center = new Point2D(3, 2);
            double radius = 2;
            Circle circle = new Circle(center, radius);

            Point2D firstIntersectionPoint = new Point2D(3, 4);
            Point2D secondIntersectionPoint = new Point2D(5, 2);
            LineSegment2D lineSegment = new LineSegment2D(firstIntersectionPoint, secondIntersectionPoint);

            List<Point2D> intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Contains(firstIntersectionPoint));
            Assert.IsTrue(intersectionPoints.Contains(secondIntersectionPoint));

            Point2D nonIntersectingPoint = new Point2D(4, 3);

            lineSegment = new LineSegment2D(nonIntersectingPoint, secondIntersectionPoint);
            intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.AreEqual(1, intersectionPoints.Count);
            Assert.IsTrue(intersectionPoints.Contains(secondIntersectionPoint));

            lineSegment = new LineSegment2D(firstIntersectionPoint, nonIntersectingPoint);
            intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.AreEqual(1, intersectionPoints.Count);
            Assert.IsTrue(intersectionPoints.Contains(firstIntersectionPoint));

            Point2D secondNonIntersectingPoint = new Point2D(4.5, 2.5);
            lineSegment = new LineSegment2D(nonIntersectingPoint, secondNonIntersectingPoint);
            intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.AreEqual(0, intersectionPoints.Count);
        }

        /// <summary>
        /// Test intersection of line segment and circle at one point
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void LineSegment2DCircleAtOnePointIntersectionTest()
        {
            Point2D center = new Point2D(3, 2);
            double radius = 2;
            Circle circle = new Circle(center, radius);

            Point2D intersectionPoint = new Point2D(3, 4);
            Point2D secondSegmentPoint = new Point2D(4, 4);

            LineSegment2D lineSegment = new LineSegment2D(intersectionPoint, secondSegmentPoint);

            List<Point2D> intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Contains(intersectionPoint));
            Assert.IsTrue(intersectionPoints.Count == 1);

            lineSegment = new LineSegment2D(intersectionPoint + new Vector2(0.5, 0), secondSegmentPoint);
            intersectionPoints = lineSegment.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Count == 0);
        }

        /// <summary>
        /// Test intersection of line segment and line segment
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void LineSegment2DLineSegmentIntersectionTest()
        {
            LineSegment2D lineSegment1 = new LineSegment2D(new Point2D(-3, 0), new Point2D(1, -4));
            LineSegment2D lineSegment2 = new LineSegment2D(new Point2D(-3, -4), new Point2D(2, -2));

            List<Point2D> intersections1 = lineSegment1.GetIntersectionPoints(lineSegment2);
            List<Point2D> intersections2 = lineSegment2.GetIntersectionPoints(lineSegment1);
            List<Point2D> intersections3 = lineSegment2.GetIntersectionPoints(lineSegment2);

            Assert.AreEqual(1, intersections1.Count);
            Assert.AreEqual(1, intersections2.Count);
            Assert.AreEqual(intersections1[0], intersections2[0]);
            Assert.AreEqual(0, intersections3.Count);

            LineSegment2D lineSegment3 = new LineSegment2D(new Point2D(-3, 0), new Point2D(-1, -2));
            List<Point2D> intersections4 = lineSegment3.GetIntersectionPoints(lineSegment2);
            Assert.AreEqual(0, intersections4.Count);
        }
    }
}
